<?php

namespace app\backend\service;

use app\common\model\DoubleModel;
use app\common\RootService;

class DoubleService extends RootService
{
    /**
     * 标签映射
     * @return array
     */
    public function dataLabel()
    {
        return (new MoneyRecordService())->coinLabel();
    }

    /**
     * 列表
     * @param array $params
     */
    public function lists(array $params)
    {
        $dataLabel = $this->dataLabel();
        // 数据源
        $doubleList = $this->page($this->doubleModel($params)->order(['did desc']));
        // 数据处理
        array_walk($doubleList['data'], function (&$row) use ($dataLabel) {
            $row['bindType'] = $dataLabel['bindType'][$row['bind_type']];
            $row['methodLabel'] = $dataLabel['methodLabel'][$row['type']] ?? '--';
        });
        return $doubleList;
    }

    /**
     * 流水记录模型
     * @param array $params
     * @return MoneyRecordModel
     */
    protected function doubleModel(array $params)
    {
        // 用户ID筛选
        condition($params['invite'] ?? '') && $where[] = ['m.invite', '=', $params['invite']];
        // 日期筛选
        empty($params['writeDate']) || $where[] = ['db.create_time', 'between', betweenDate($params['writeDate'])];
        // 金币来源筛选
        condition($params['method'] ?? '') && $where[] = ['db.type', 'in', explode(',', $params['method'])];
        // 筛选字段
        $fields = ['db.id did', 'db.member_id', 'db.amount', 'db.type', 'db.create_time', 'm.avatar', 'm.invite',
            'm.account', 'm.bind_type', 'm.email', 'm.id', 'db.record_id', 'm.state memberState'];
        // 筛选结果
        return DoubleModel::alias('db')->join(['sys_member' => 'm'], 'db.member_id = m.id', 'left')->field($fields)
            ->where($where ?? []);
    }
}